package edu.upb.acs.jobsmaps.activity;

import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.speech.SpeechRecognizer;
 import android.speech.tts.TextToSpeech;
 import android.speech.tts.TextToSpeech.OnInitListener;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 import edu.upb.acs.jobsmaps.R;
 import edu.upb.acs.jobsmaps.activity.voiceRecognition.VoiceRecognitionListenerCompany;
 import edu.upb.acs.jobsmaps.database.task.AddCompanyTask;
 import edu.upb.acs.jobsmaps.entity.Company;
 import edu.upb.acs.jobsmaps.voice.VoiceUtils;

public class AddCompanyActivity extends Activity implements OnInitListener {
	 final Context context = this;
	 private Button addButton;
	 private static final String TAG = "AddCompanyActivity";
	 private SpeechRecognizer sr;
	 private Intent intent;
	 private TextToSpeech tts;
	 private AudioManager amAudioManager;


	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.addcompany);

		 // components from main.xml
		 addButton = (Button) findViewById(R.id.addCompanyButtonAdd);

		 // add button listener
		 addButton.setOnClickListener(new OnClickListener() {

			 @Override
			 public void onClick(View arg0) {
				 createCompany();

				 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						 context);
				 alertDialogBuilder.setMessage("Company was added with success");
				 alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
						 new DialogInterface.OnClickListener() {
							 public void onClick(DialogInterface dialog, int id) {
								 // when the ok button is pushed, return to main
								 // view
								 setResult(RESULT_OK);
								 finish();
							 }
						 }
				 );

				 // create alert dialog
				 AlertDialog alertDialog = alertDialogBuilder.create();
				 // show it
				 alertDialog.show();
			 }
		 });

		 Button goBackButton = (Button) findViewById(R.id.addCompanyButtonCancel);
		 goBackButton.setOnClickListener(new OnClickListener() {
			 public void onClick(View arg0) {
				 goToMainActivity();
			 }
		 });

		 //continue with voice recognition

		 //the audio manager for microphone
		 amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

		 //create a new speaker
		 tts = new TextToSpeech(this, this);

		 Log.d(TAG, "Entering startListening");
		 startListening();

	 }

	 @Override
	 public void onStart() {
		 super.onStart();
		 Log.d(TAG, "In the onStart() event");

	 }

	 @Override
	 public void onDestroy() {
		 if (tts != null) {
			 tts.stop();
			 tts.shutdown();
		 }

		 sr.stopListening();
		 sr.destroy();
		 super.onDestroy();
	 }


	 private void startListening() {
		 sr = SpeechRecognizer.createSpeechRecognizer(this);

		 intent = VoiceUtils.initializeIndent(intent, getApplicationContext());

		 sr.setRecognitionListener(new VoiceRecognitionListenerCompany(sr, intent, tts, amAudioManager, this));
		 sr.startListening(intent);

	 }

	 //folosit la TextToSpeech
	 @Override
	 public void onInit(int status) {
		 if (status == TextToSpeech.SUCCESS) {
			 //Speak
			 tts.speak("Enter name", TextToSpeech.QUEUE_FLUSH, null);
			 while (tts.isSpeaking()) {
				 //Log.d(TAG, "She is speaking");
			 }
		 } else {
			 //Handle initialization error here
			 Log.d(TAG, "Initialization for TTS failed!!!");
		 }
	 }


	 private void createCompany() {
		 final Company company = new Company();

		 final EditText nameField = (EditText) findViewById(R.id.addCompanyName);
		 String name = nameField.getText().toString();
		 Log.d(TAG, "company name = >" + name + "<");
		 company.setName(name);

		 String description = ((EditText) findViewById(R.id.addCompanyDescription)).getText().toString();
		 Log.d(TAG, "company description = >" + description + "<");
		 company.setDescription(description);

		 String profile = ((Spinner) findViewById(R.id.addCompanyProfile)).getSelectedItem().toString();
		 if (!profile.equalsIgnoreCase("any")){
			 company.setProfile(profile);
		 }
		 Log.d(TAG, "company profile = >" + profile + "<");

		 Double latitude = Double.valueOf(((EditText) findViewById(R.id.addCompanyLatitude)).getText().toString());
		 company.setLatitude(latitude);
		 Log.d(TAG, "company latitude = >" + latitude + "<");

		 Double longitude = Double.valueOf(((EditText) findViewById(R.id.addCompanyLongitude)).getText().toString());
		 company.setLongitude(longitude);
		 Log.d(TAG, "company longitude = >" + longitude + "<");

		 String addCompanyUrl = ((EditText) findViewById(R.id.addCompanyUrl)).getText().toString();
		 Log.d(TAG, "company url = >" + description + "<");
		 company.setCompanyUrl(addCompanyUrl);

		 AddCompanyTask addCompanyTask = new AddCompanyTask();
		 try {
			 Integer persistedCompanyId = addCompanyTask.execute(company).get();
			 Log.d(TAG, "New company id = " + persistedCompanyId);
		 } catch (Exception e) {
			 Log.e(TAG, "Can not persist company", e);
		 }
	 }

	 private void goToMainActivity() {
		 setResult(RESULT_OK);
		 finish();
	 }
 }
